package com.example.fw;

import static org.junit.Assert.fail;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class WebDriverHelper {
	private static WebDriver driver;
	private StringBuffer verificationErrors = new StringBuffer();
	
	public WebDriverHelper(ApplicationManager manager) {
		String browser = manager.getProperty("browser", "htmlunit");
		if ("firefox".equals(browser)) driver = new FirefoxDriver();
		if ("ie".equals(browser)) driver = new InternetExplorerDriver();
		if ("chrome".equals(browser)) driver = new ChromeDriver();
		if ("htmlunit".equals(browser)) driver = new HtmlUnitDriver();
		driver.manage().timeouts().implicitlyWait(Integer.parseInt(manager.getProperty("implicitlyWait", "10")), TimeUnit.SECONDS);	
		driver.get(manager.getProperty("baseUrl", "http://localhost/addressbookv4.1.4/"));		
	}
	
	public void stop() {
		driver.quit();
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString)) {
			fail(verificationErrorString);
		}
	}
	
	public WebDriver getDriver(){
		return driver;
	}

}
